var request = require("superagent");
var API = require("./support/const")
var service = require("../../service/log")
var fs = require('fs');

/*********************************************************辅导管理*************************/

/**
 * 获取辅导列表
 * @param req
 * @param res
 * @param next
 */
exports.getInstructorList = function (req, res, next) {
    let _res = res
    let _req = req
    let{page,name,state} = _req.body;

    let header = {
        token: req.cookies.token
    };
    request.get(`${API.enjoylove.instructor}`)
        .query({page:page})
        .query({name:name})
        .query({state:state})
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '获取辅导列表'}, _req);
            }
            _res.json(res.text)
        })

}
/**获取区域信息
 *
 * @param req
 * @param res
 * @param next
 */
exports.getInstructorAreas =function (req, res, next) {
    let _res = res
    let _req = req

    let header = {
        token: req.cookies.token
    };
    request.get(`${API.enjoylove.area}`)
        .query({})
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '获取区域信息'}, _req);
            }
            _res.json(res.text)
        })
}

/**
 * 添加辅导
 * @param req
 * @param res
 * @param next
 */
exports.addInstructor =function (req, res, next) {
    let _res = res
    let _req = req
    let{mobile,name,state,areas} = _req.body;

    let header = {
        token: req.cookies.token
    };
    request.post(`${API.enjoylove.instructor}`)
        .query({mobile:mobile})
        .query({name:name})
        .query({state:state})
        .query({areas:areas})
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '添加辅导信息'}, _req);
            }
            _res.json(res.text)
        })
}
/**
 * 更新辅导信息
 * @param req
 * @param res
 * @param next
 */
exports.updateInstructor =function (req, res, next) {
    let _res = res
    let _req = req
    let{mobile,name,state,areas,id} = _req.body;

    let header = {
        token: req.cookies.token
    };
    request.post(`${API.enjoylove.instructor}/${id}`)
        .query({mobile:mobile})
        .query({name:name})
        .query({state:state})
        .query({areas:areas})
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '更新辅导信息'}, _req);
            }
            _res.json(res.text)
        })
}

/*********************************************************客户管理*************************/

/**
 * 获取客户信息列表
 * @param req
 * @param res
 * @param next
 * 已授权
 */
exports.getCustomerList = function (req,res,next) {
  let _res = res
  let _req = req
  let{page,name,instructor,area,state} = _req.body;
    let params ={
        page:page,
        name:name,
        instructor:instructor,
        area:area,
        state:state
    }
  let header = {
    token: req.cookies.token
  };
  request.get(`${API.enjoylove.customer}`)
    .query(params)
    .set(header)
    .end(function (req, res) {
      //记录用户操作日志
      if (res.status == 200) {
        service.save_user_log({'type': 'getList', 'msg': '获取客户列表'}, _req);
    }
      _res.json(res.text)
    })
}

/*********************************************************商品管理*************************/

/**
 * 获取商品信息列表
 * @param req
 * @param res
 * @param next
 */
exports.getProductList = function (req,res,next) {
  let _res = res
  let _req = req
  let {page, category, title, barCode, shelfState} = _req.body;
  let params = {
    "page": page,
    "category": category,
    "title": title,
    "barCode": barCode,
    "shelfState": shelfState
  };
  let header = {
    token: req.cookies.token
  };
  request.get(`${API.enjoylove.product}`)
    .query(params)
    .set(header)
    .end(function (req, res) {
      //记录用户操作日志
      if (res.status == 200) {
        service.save_user_log({'type': 'getList', 'msg': '商品信息列表_'+title}, _req);
      }
      _res.json(res.text)
    })

}

/**
 * 商品上传
 * @param req
 * @param res
 * @param next
 */

var fs = require('fs');
exports.uploadProduct = function (req,res,next) {

  let _res = res
  let _req = req
  let header = {
    token: req.cookies.token
  };

  if (_req.file) {
    let {} = _req.body;
    // _res.send('文件上传成功');

    request.post(`${API.enjoylove.product}/upload/zip`)
        .set(header)
        .field(req.body)
        .attach('file', _req.file.path)
        .end(function (req, res) {
         // if(_res.success){
            //记录用户操作日志
            if (res.status == 200) {
              service.save_user_log({'type': 'del', 'msg': '上传商品'}, _req);
            }
            fs.unlink(_req.file.path);
            console.log(res.text)
            _res.json(res.text)

         // }
        });
  }

}

/**
 * 删除商品图片
 * @param req
 * @param res
 * @param next
 */
exports.deleteProductPicture = function (req,res,next) {
  let _res = res
  let _req = req
  let {id, attIds} = _req.body;
  let header = {
    token: req.cookies.token
  };

  request.post(`${API.enjoylove.product}/pic/delete/${id}`)
      .query({attIds:attIds})
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'del', 'msg': '删除的商品图片_'+id}, _req);
        }
        _res.json(res.text)
      })
}

/**
 * 商品删除
 * @param req
 * @param res
 * @param next
 */
exports.deleteProductCategory = function (req,res,next) {

  let _res = res
  let _req = req
  let {ids} = _req.body;
  let header = {
    token: req.cookies.token
  };
  request.post(`${API.enjoylove.product}/remove-category`)
    .query({ids:ids})
    .set(header)
    .end(function (req, res) {
      //记录用户操作日志
      if (res.status == 200) {
        service.save_user_log({'type': 'del', 'msg': '删除的商品_'+ids}, _req);
      }
      _res.json(res.text)
    })
}
/**
 * 添加商品
 * @param req
 * @param res
 * @param next
 */
exports.addProductCategory = function (req,res,next) {
  let _res = res
  let _req = req
  let {ids, categoryId} = _req.body;
  let params = {
    "ids": ids,
    "categoryId": categoryId
  };
  let header = {
    token: req.cookies.token
  };

  request.post(`${API.enjoylove.product}/add-category`)
    .query(params)
    .set(header)
    .end(function (req, res) {
      //记录用户操作日志
      if (res.status == 200) {
        service.save_user_log({'type': 'add', 'msg': '添加的商品_'+ids}, _req);
      }
      _res.json(res.text)
    })


}
/**
 * 指定分类商品列表
 * @param req
 * @param res
 * @param next
 */
exports.productCategoryList = function (req,res,next) {
  let _res = res
  let _req = req
  let {page, title, barCode, categoryId} = _req.body;
  let params = {
    "page": page,
    "title": title,
    "barCode": barCode,
    "category": categoryId
  };
  let header = {
    token: req.cookies.token
  };

  request.get(`${API.enjoylove.product}/category`)
      .query(params)
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'getList', 'msg': '指定分类商品信息列表_'+categoryId}, _req);
        }
        _res.json(res.text)
      })
}

/**
 * 没有分类商品列表
 * @param req
 * @param res
 * @param next
 */
exports.productAnyCategoryList = function (req,res,next) {
  let _res = res
  let _req = req
  let {page, title, barCode} = _req.body;
  let params = {
    "page": page,
    "title": title,
    "barCode": barCode
  };
  let header = {
    token: req.cookies.token
  };
  request.get(`${API.enjoylove.product}/category-non`)
      .query(params)
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'getList', 'msg': '未分类商品信息列表_'+title}, _req);
        }
        _res.json(res.text)
      })
}
/**
 * 添加商品图片
 * @param req
 * @param res
 * @param next
 */
exports.addProductPicture = function (req,res,next) {

  let header = {
    token: req.cookies.token
  };
  if (req.file) {
    let {id} = req.body;

    request.post(`${API.enjoylove.product}/pic/${id}`)
        .set(header)
        .attach('file', req.file.path)
        .end(function (_req, _res) {
//记录用户操作日志
          if (res.status == 200) {
            service.save_user_log({'type': 'del', 'msg': '添加商品图片_'+id}, _req);
          }
           res.json(_res.text)
        });
  }
}
/**
 * 编辑商品
 * @param req
 * @param res
 * @param next
 */
exports.editProduct = function (req,res,next) {
  let _res = res
  let _req = req
  let{id, title, barCode, spec, unit, perCarton, fleetSales, shelfState, price} = _req.body;
  let params = {
    'title': title,
    'barCode': barCode,
    'spec': spec,
    'unit': unit,
    'perCarton': perCarton,
    'fleetSales': fleetSales,
    'shelfState': shelfState,
    'price': price
  };
  let header = {
    token: req.cookies.token
  };

  request.post(`${API.enjoylove.product}/${id}`)
      .query(params)
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'edit', 'msg': '修改商品_'+id}, _req);
        }
        _res.json(res.text)
      })
}
/**
 * 编辑商品单位
 * @param req
 * @param res
 * @param next
 */
exports.editProductUnit = function (req,res,next) {
  let _res = res
  let _req = req
  let params = {};
  let header = {
    token: req.cookies.token
  };

  request.get(`${API.enjoylove.product}/unit`)
      .query(params)
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'getList', 'msg': '查询商品单位'}, _req);
        }
        _res.json(res.text)
      })
}


/**
 * 商品详情
 * @param req
 * @param res
 * @param next
 */
exports.getProduct = function (req,res,next) {
  let _res = res
  let _req = req
  let{id} = _req.body;
  let params = {
    'id': id
  };

  let header = {
    token: req.cookies.token
  };

  request.get(`${API.enjoylove.product}/${id}`)
      .query(params)
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'getList', 'msg': '查询商品详情'}, _req);
        }
        _res.json(res.text)
      })
}

/*********************************************************分类管理*************************/

/**
 * 获取分类管理列表
 * @param req
 * @param res
 * @param next
 */
exports.getCategoryList = function (req,res,next) {

    let _res = res
    let _req = req

    let header = {
        token: req.cookies.token
    };
    request.get(`${API.enjoylove.category}`)
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '查询分类列表'}, _req);
            }
            _res.json(res.text)
        })
}
/**
 * 新增分类管理
 * @param req
 * @param res
 * @param next
 */
exports.addCategory = function (req,res,next) {
  let _res = res
  let _req = req
  let{title, code, sortNum, parent} = _req.body;
  let params = {
    "title": title,
    "code": code,
    "sortNum": sortNum,
    "parent": parent
  };
  let header = {
    token: req.cookies.token
  };

  request.post(`${API.enjoylove.category}`)
      .query(params)
      .set(header)
      .end(function (req, res) {
        //记录用户操作日志
        if (res.status == 200) {
          service.save_user_log({'type': 'add', 'msg': '新增分类列表_'+title}, _req);
        }
        _res.json(res.text)
      })
}

/**
 * 修改分类管理
 * @param req
 * @param res
 * @param next
 */
exports.updateCategory = function (req,res,next) {
  let _res = res
  let _req = req
  let{title, sortNum, id} = _req.body;
  let params = {
    "title": title,
    "sortNum": sortNum
  };
  let header = {
    token: req.cookies.token
  };

  request.post(`${API.enjoylove.category}/${id}`)
    .query(params)
    .set(header)
    .end(function (req, res) {
      //记录用户操作日志
      if (res.status == 200) {
        service.save_user_log({'type': 'edit', 'msg': '修改分类_'+title}, _req);
      }
      _res.json(res.text)
    })
}

/*********************************************************订单管理*************************/

/**
 * 获取订单列表
 * @param req
 * @param res
 * @param next
 */
exports.getOrderList = function (req,res,next) {
    let _res = res
    let _req = req
    let{orderNo, shopName, orderStatus,startDate,endDate,page} = _req.body;
    let params = {
        orderNo:orderNo,
        shopName:shopName,
        orderStatus:orderStatus,
        startDate:startDate,
        endDate:endDate,
        page:page
    };
    let header = {
        token: req.cookies.token
    };
    request.get(`${API.enjoylove.order}`)
        .query(params)
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '获取订单列表'}, _req);
            }
            _res.json(res.text)
        })
}
/**
 * 获取订单状态集合
 * @param req
 * @param res
 * @param next
 */
exports.getOrderStatusList=function (req,res,next) {
    let _res = res
    let _req = req

    let header = {
        token: req.cookies.token
    };
    request.get(`${API.enjoylove.order}/status`)
        .query({})
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': '获取全部订单状态列表'}, _req);
            }
            _res.json(res.text)
        })

}
/**
 * 订单详情
 * @param req
 * @param res
 * @param next
 */
exports.getOrderDetailByID=function (req,res,next) {
    let _res = res
    let _req = req
    let{id} = _req.body;
    let header = {
        token: req.cookies.token
    };
    request.get(`${API.enjoylove.order}/${id}`)
        .set(header)
        .end(function (req, res) {
            //记录用户操作日志
            if (res.status == 200) {
                service.save_user_log({'type': 'getList', 'msg': `获取订单详情'+${id}`}, _req);
            }
            _res.json(res.text)
        })

}
/***
 * 确认订单
 * @param req
 * @param res
 * @param next
 */
exports.confirmOrder=function (req,res,next) {
    if (req.file) {
        let _res = res;
        let _req = req;
        let {id, products} = _req.body;
        let file = req.file;
        let header = {
            token: req.cookies.token
        };
        console.log(`${API.enjoylove.order}/${id}`);
        request.post(`${API.enjoylove.order}/${id}`)
            .set(header)
            .field('products', products)
            .attach('file', file.path)
            .end(function (req, res) {
                //记录用户操作日志
                if (res.status == 200) {
                    service.save_user_log({'type': 'getList', 'msg': `确认订单'+${id}`}, _req);
                }
                _res.json(res.text)
            });
    }
}
/**
 * 上传收货单
 * @param req
 * @param res
 * @param next
 */
exports.uploadOrderPic = function (req,res,next) {

    let header = {
        token: req.cookies.token
    };

    if (req.file) {
        let {orderId} = req.body;

        request.post(`${API.enjoylove.order}/${orderId}`)
            .set(header)
            .attach('file', req.file.path)
            .end(function (_req, _res) {
                //记录用户操作日志
                if (res.status == 200) {
                    service.save_user_log({'type': 'add', 'msg': '上传收货单'+id}, _req);
                }
               // fs.unlink(req.file.path);
                console.log(_res.text)
                res.json(_res.text)
            });
    }
}